package com.ruoyi.charge.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.charge.auth.service.LoginTokenService;
import com.ruoyi.common.utils.ServletUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.charge.vo.AgentVo;
import com.ruoyi.charge.bo.AgentQueryBo;
import com.ruoyi.charge.bo.AgentAddBo;
import com.ruoyi.charge.bo.AgentEditBo;
import com.ruoyi.charge.service.IAgentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 代理商管理Controller
 *
 * @author fanqie
 * @date 2021-10-25
 */
@Api(value = "代理商管理控制器", tags = {"代理商管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/charge/agent")
public class AgentController extends BaseController {

    private final IAgentService iAgentService;
    private final LoginTokenService loginTokenService;

    /**
     * 查询代理商管理列表
     */
    @ApiOperation("查询代理商管理列表")
    @GetMapping("/list")
    public TableDataInfo<AgentVo> list(AgentQueryBo bo) {
        startPage();
        List<AgentVo> list = iAgentService.queryList(bo);
        return getDataTable(list);
    }


    /**
     * 获取代理商管理详细信息
     */
    @ApiOperation("获取代理商管理详细信息")
    @GetMapping("")
    public AjaxResult<AgentVo> getInfo() {
        Long userId = loginTokenService.getLoginUserId(ServletUtils.getRequest());
        return AjaxResult.success(iAgentService.queryById(userId));
    }

    /**
     * 修改代理商管理
     */
    @ApiOperation("修改代理商管理")
    @Log(title = "代理商管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody AgentEditBo bo) {
        Long userId = loginTokenService.getLoginUserId(ServletUtils.getRequest());
        bo.setAgentId(userId);
        return toAjax(iAgentService.updateByEditBo(bo) ? 1 : 0);
    }

}
